#!/usr/bin/env bash

#get this script real directory
SOURCE="$0"
while [ -h "$SOURCE"  ]; do # resolve $SOURCE until the file is no longer a symlink
    this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    # if $SOURCE was a relative symlink, 
    #we need to resolve it relative to the path 
    #where the symlink file was located
    [[ $SOURCE != /*  ]] && SOURCE="$this_script_dir/$SOURCE"
done
this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
source $this_script_dir/global.sh

#$this_script_dir/install_expect.sh
if [ -f ~/.profile ] ; then
    source ~/.profile
fi

################################################################################
#                                                                          80col
print_help() 
{
    cat <<EOF
    useage: 
    $0 --username=<root> ...

    notice: parameter's number must >= 5

    --username=<root>   - the name of remote host
    --host_ip=<ip>      - remoet's ip
    --passwd=<passwd>   - remote user's login passworld
    --ssh_port=<port>   - remote host's port of  open ssh (default 22) 
    --cmd=<command>     - <command>/scritp that user want to execute on remote host 
    --log_level=<debug> - control pint log on streen,trace|debug|info|warn|error|critical

    --stop  - stop accesspoint
    --start - start accesspoint
    --help  - prints help screen, script will exit after printing


EOF
}

parse_arguments "$@" #must have "" on $@ ,or result in wrong num of args in function

if [ -n "$help" ] || [ $# -eq 0 ]; then
    print_help
    exit 0
fi

#1:username 
#2:host_ip
#3:passwd
#4:ssh_port
#5:cmd

check_argument username host_ip cmd || exit 1

check_ip_valid $host_ip || exit 1

if [ -z "$ssh_port" ]; then
    ssh_port=22
fi

vlogd "start run: \"$0 $*\""
vlogd "remote host: $username@$host_ip" 
vlogd "remote port: $ssh_port" 
vlogd "remote run cmd: $cmd" 

export cmd_exp="$cmd"  #append cmd after ssh need not exit 

#${passwd//$/\\$} \  #replace $ to \$


#user want to log out on screen , when log_level set to debug or more low
if [ $cmd_log_to_screen == true ]; then
    $this_script_dir/remote_run.exp $username $host_ip \
        $ssh_port \
        ${passwd}

       #2>&1| tee -a $log_file #can not use tee,or $? will to be tee
       #>> $log_file
       #| tee -a $log_file
       #>> $log_file 2>&1

       # > /dev/null 2>&1
   else
       $this_script_dir/remote_run.exp $username $host_ip \
           $ssh_port \
           ${passwd} &>> $log_file
fi
ret=$?   #mark return code

unset cmd_exp

#log out some error message about failed
if [ $ret -ne 0 ] ; then 
    if [ $ret -eq 3 ] ; then
        reason_str="Permission denied passwd not right"
    elif [ $ret -eq 130 ] ; then
        reason_str="user interrupt execution by ctrl-c"
    elif [ $ret -eq 255 ] ; then
        reason_str="please check ip/port"
    else
        reason_str="cmd failed, please check ~/git/run/log/shell_global.log"
    fi

    vloge "run cmd failed on $username@$host_ip:$ssh_port;" \
        "return:$ret;"   \
        "reason:$reason_str;" \
        "cmd is:\"$cmd\""
fi
 
exit $ret



